<template>
  <el-dialog :title="title" :visible.sync="open" width="70%" :before-close="handleClose" class="dialogClass">
    <el-form ref="ruleForm" :rules="rules" :model="ruleForm" label-width="80px">
      <el-row :gutter="20">
        <el-col :xl="8" :lg="8">
          <el-form-item label="账号" prop="id">
            <el-input size="big" v-model="ruleForm.id" placeholder="请输入账号" :disabled="!controlShow"
              clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="8" :lg="8">
          <el-form-item label="充值金额" prop="balance">
            <el-input size="big" v-model="ruleForm.balance" placeholder="请输入充值金额" :disabled="!controlShow"
              clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="big" @click="onSubmit('ruleForm')" :loading="loading">{{
        submitButton
      }}</el-button>
      <el-button size="big" @click="resetForm('ruleForm')" v-if="restBtn">重置</el-button>
      <el-button size="big" @click="close('ruleForm')">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { openAccount,recharge} from '@/apis/client/clientManagement.js'

export default {
  name: "recharge",
  data() {
    return {
      // 委派和员工的显示切换标志
      controlShow: true,
      // 重置按钮的显示切换标志
      restBtn: true,
      // 提交按钮
      submitButton: '',
      // 控制弹窗
      open: false,
      // 控制按钮加载状态
      loading: false,
      // 弹窗标题
      title: '充值',
      // 店铺下拉
      options: null,
      // 表单项
      ruleForm: {
        id: '',
        balance: '',
      },
      nullruleForm: {
        id: '',
        balance: '',
      },

      // 表单校验
      rules: {
        id: [{ required: true, message: '请输入充值账号', trigger: 'blur' }],
        balance: [{ required: true, message: '请输入充值金额', trigger: 'blur' }],
      },

    }
  },
  created() {

  },
  methods: {
    // 控制弹窗

    show(title, row) {
      const _this = this
      _this.title = title;
      _this.open = true;

        _this.submitButton = "充值";
      
    },

    close(formName) {
      this.open = false;
      this.resetForm(formName);
    },
    handleClose(done) {
      const _this = this;
      _this.$confirm('系统可能不会保存您的更改', '确认关闭？')
        .then(_ => {
          done();
          _this.resetForm('ruleForm');
        })
        .catch(_ => { });
    },
    onSubmit(formName) {
      const _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
              console.log(_this.title);

            if (_this.title == '充值') {
              // console.log(_this.ruleForm);
              recharge(_this.ruleForm).then(function (res) {
                if (res.code === 0) {
                  _this.$alert(res.msg, '提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                      _this.close('ruleForm');
                      _this.$parent.$parent.search()
                    }
                  })
                }
              })
             } 
        }
      });
    },
    // 表单重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.ruleForm = this.nullruleForm
    }
  }
}
</script>

